# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Sergej Pupykin <pupykin.s+arch@gmail.com>
# Contributor: tuxbubling <tuxbubling@jabber.fr>

pkgname=cacti
pkgver=1.2.25
pkgrel=1
pkgdesc="Network graphing solution using RRDTool"
arch=(any)
url="https://www.cacti.net"
license=(GPL-2.0-or-later)
depends=(
  php
  php-gd
  php-snmp
  rrdtool
  ttf-dejavu
)
optdepends=(
  'mariadb: use local MySQL server'
  'perl: for scripts'
  'php-fpm: run in fastCGI process manager'
  'uwsgi-plugin-php: run as local application container'
)
backup=(
  etc/webapps/cacti/.htaccess
  etc/webapps/cacti/config.php
)
source=(
  https://www.$pkgname.net/downloads/$pkgname-$pkgver.tar.gz
  $pkgname.uwsgi
  $pkgname.sysusers
  $pkgname.tmpfiles
)
install=$pkgname.install
sha512sums=('15dec723102343b4457ef2baeb98789125c69cceb52f6baac1d002dde42e844da5189fd23f77fe1e72f0e80ee6d329d0df653af18e1f624012f00fbaaee62f34'
            'ba421785a3a86535dba210b5ac34ad059fac72b809a9027308d047a967de43aa204eda56e129b28f6400859427de123734b338c5d8cd3da1944893fa3ef33e48'
            '847e2b791de44d0790a2fdb81c77c8af9a66da9d44500f3f8a8d1c0f406d3a20082cc8fef1c6afe4de93ad989d35c79c9809abe14693a9ac6ea74d4696e3b6c1'
            'e833e411f74e77773c32589ba83cb1b2f28ca9b35931626480ab7daa63420d47ecfc3061e6703323646b69e1d98536b6f3afdd36faa483fb13aac9b818af0c6e')
b2sums=('606419d31b3fb4cc0bcb82c1b6de355686e45b59de59b52bf213f5435082ad3691dd5cde769ef3cd71425bd8ec6a7a6296d9d6f5470a88a56682a8fec9d61e90'
        '880cfc1a87abd95a624ffef6f1ae5ac416b4020ae40edc32355314bb33b5003b21ab8ed082b520d70b166739a1b056339cca7b6a6bb82167c14925ae3ca0d34f'
        '2ec9956b690ab9244e31a58d295ab56b5d0df9fc9586c74edbd55c12d3383430b8a1a8a708d7d747abadb67eee42094562da510ddfc7797978c2683e7b86a252'
        '0a532bbbd07b21da18ea21eec3f268510740069fb732e3d387f224b93f0539e3e968ba6332bf647ff62f2d98910abc33e35a3e82d335cf7d29a503609e22651f')

prepare() {
  # adding default .htaccess
  echo "Require all denied" > $pkgname-htaccess

  cd $pkgname-$pkgver
  # setting correct install path for spine
  sed -e 's|/usr/local/spine/bin/spine|/usr/bin/spine|g' -i install/functions.php
  # setting correct path for dejavu font
  sed -e 's|/usr/share/fonts/dejavu/|/usr/share/fonts/TTF/|g' -i lib/rrd.php
  # remove unneeded executable bits
  find . -executable -type f -and -not -path "*scripts*" -exec chmod -c 644 {} \;
}

package() {
  cd $pkgname-$pkgver

  # install webapp dir
  install -vdm 755 "$pkgdir/usr/share/webapps/$pkgname/include/"
  # config
  install -vDm 640 include/config.php -t "$pkgdir/etc/webapps/$pkgname/"
  install -vDm 644 ../$pkgname-htaccess "$pkgdir/etc/webapps/$pkgname/.htaccess"
  ln -sv /etc/webapps/$pkgname/.htaccess "$pkgdir/usr/share/webapps/$pkgname/.htaccess"
  ln -sv /etc/webapps/$pkgname/config.php "$pkgdir/usr/share/webapps/$pkgname/include/config.php"
  # state
  install -vdm 750 "$pkgdir/var/lib/$pkgname/"
  install -vDm 644 rra/.htaccess -t "$pkgdir/var/lib/$pkgname/rra/"
  ln -sv /var/lib/$pkgname/rra "$pkgdir/usr/share/webapps/$pkgname/rra"
  install -vDm 644 resource/index.php -t "$pkgdir/var/lib/$pkgname/resource/"
  install -vDm 644 resource/snmp_queries/*.{php,xml} -t "$pkgdir/var/lib/$pkgname/resource/snmp_queries/"
  install -vDm 644 resource/script_server/*.{php,xml} -t "$pkgdir/var/lib/$pkgname/resource/script_server/"
  install -vDm 644 resource/script_queries/*.{php,xml} -t "$pkgdir/var/lib/$pkgname/resource/script_queries/"
  ln -sv /var/lib/$pkgname/resource "$pkgdir/usr/share/webapps/$pkgname/resource"
  install -vDm 644 scripts/*.{php,pl,sh} -t "$pkgdir/var/lib/$pkgname/scripts/"
  ln -sv /var/lib/$pkgname/scripts "$pkgdir/usr/share/webapps/$pkgname/scripts"
  # NOTE: add symlinks required for scripts
  ln -sv /usr/share/webapps/$pkgname/lib "$pkgdir/var/lib/$pkgname/"
  ln -sv /usr/share/webapps/$pkgname/include "$pkgdir/var/lib/$pkgname/"
  # cache
  install -vdm 750 "$pkgdir/var/cache/$pkgname/"
  install -vDm 644 cache/boost/*.php -t "$pkgdir/var/cache/$pkgname/boost/"
  install -vDm 644 cache/mibcache/*.php -t "$pkgdir/var/cache/$pkgname/mibcache/"
  install -vDm 644 cache/realtime/*.php -t "$pkgdir/var/cache/$pkgname/realtime/"
  install -vDm 644 cache/spikekill/*.php -t "$pkgdir/var/cache/$pkgname/spikekill/"
  ln -sv /var/cache/$pkgname "$pkgdir/usr/share/webapps/$pkgname/cache"
  # log
  install -vdm 750 "$pkgdir/var/log/$pkgname"
  install -vDm 644 log/.htaccess -t "$pkgdir/var/log/$pkgname"
  ln -sv /var/log/$pkgname "$pkgdir/usr/share/webapps/$pkgname/log"
  # docs
  install -vDm 644 {CHANGELOG,README.md} -t "$pkgdir/usr/share/doc/$pkgname/"
  # tmpfiles.d
  install -vDm 644 ../$pkgname.tmpfiles "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"
  # sysusers.d
  install -vDm 644 ../$pkgname.sysusers "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
  # uwsgi config
  install -vDm 644 ../$pkgname.uwsgi "$pkgdir/etc/uwsgi/$pkgname.ini"

  # remove already installed files/folder so that they are not installed again
  rm -frv include/config.php rra/ resource/ scripts/ cache/ log/ {CHANGELOG,README.md}
  # webapp
  find . -type f -exec install -vDm 644 {} "$pkgdir/usr/share/webapps/$pkgname/"{} \;
}
